package com.power.using.mytoutiao.module.base;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Lifecycle;

import com.afollestad.materialdialogs.color.CircleView;
import com.power.using.mytoutiao.R;
import com.power.using.mytoutiao.constant.Constant;
import com.power.using.mytoutiao.util.SettingUtil;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrInterface;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

/**
 * Created by 聪明一只哈 on 2020/4/2.
 * Time: 2020/4/2  13:53
 */
public abstract class BaseActivity extends AppCompatActivity {


    private SlidrInterface slidrInterface;
    private int iconType = -1;
    private Context mContext;

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title){

        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
    }


    protected void initSlidable(){
        int isSlidable = SettingUtil.getInstance().getSlidable();
        if(isSlidable!= Constant.SLIDABLE_DISABLE){

            SlidrConfig config = new SlidrConfig.Builder()
                    .edge(isSlidable==Constant.SLIDABLE_EDGE)
                    .build();

            slidrInterface = Slidr.attach(this, config);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.iconType=SettingUtil.getInstance().getCustomIconValue();
        this.mContext=this;

        initSlidable();
    }

    @Override
    protected void onResume() {
        super.onResume();

        int color = SettingUtil.getInstance().getColor();
        int drawable = Constant.ICONS_DRAWABLES[SettingUtil.getInstance().getCustomIconValue()];
        if (getSupportActionBar() != null)
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(CircleView.shiftColorDown(color));
            // 最近任务栏上色
            ActivityManager.TaskDescription tDesc = new ActivityManager.TaskDescription(
                    getString(R.string.app_name),
                    BitmapFactory.decodeResource(getResources(), drawable),
                    color);
            setTaskDescription(tDesc);
            if (SettingUtil.getInstance().getNavBar()) {
                getWindow().setNavigationBarColor(CircleView.shiftColorDown(color));
            } else {
                getWindow().setNavigationBarColor(Color.BLACK);
            }
        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId()==android.R.id.home){
            onBackPressed();
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {

        int count=getSupportFragmentManager().getBackStackEntryCount();
        if(count==0){
            super.onBackPressed();
        }else{
            getSupportFragmentManager().popBackStack();
        }
    }

    @Override
    protected void onStop() {

        if (iconType != SettingUtil.getInstance().getCustomIconValue()) {
            new Thread(() -> {

                String act = ".SplashActivity_";

                for (String s : Constant.ICONS_TYPE) {
                    getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this, getPackageName() + act + s),
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);
                }

                act += Constant.ICONS_TYPE[SettingUtil.getInstance().getCustomIconValue()];

                getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this, getPackageName() + act),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
            }).start();
        }


        super.onStop();

    }


    public <X>AutoDisposeConverter<X> bindAutoDispose(){
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }
}
